<template>
  <div class="login">
    <div class="box">
      <el-form ref="form" :model="form" :rules="rules">
        <h3 style="text-align: center">
          <img src="@/assets/common/login-logo.png" alt="">
        </h3>
        <el-form-item prop="mobile">
          <el-input
            v-model="form.mobile"
            prefix-icon="el-icon-s-custom"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            type="password"
            @keyup.enter.native="submit"
          />
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" @click="submit"> 登录 </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: ''
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, val, next) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(val)) return next()
              return next(new Error('请输入正确号码'))
            }
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '请输入6-12位',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions('user', ['toLogin']),
    submit() {
      this.$refs.form.validate(async(val) => {
        if (val) {
          await this.toLogin(this.form)
          // 基本取值用 query 动态取值用 params
          const _add = this.$route.query.add || '/'
          this.$router.push(_add)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background: url(~@/assets/common/login.jpg) no-repeat;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 550px;
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
      border-radius: 5px;
    }
    .el-button {
      background-color: #407ffe;
      color: #fff;
      border-width: 0;
      height: 64px;
      line-height: 32px;
      font-size: 24px;
    }
  }
}
</style>
